import api from "../../api/api";
import { ElMessage, ElMessageBox } from "element-plus";
import AMapLoader from "@amap/amap-jsapi-loader"; //高德地图
import { useRouter } from "vue-router";
import axios from "axios";
export function dictionaries(group) {
  return new Promise(function (resolve, reject) {
    let list = [];
    api
      .getListAPI("auth/tools/dictionary", {
        group: group,
      })
      .then((res) => {
        if (res.data.code == 1) {
          list = res.data.data;
          resolve(list);
        } else {
          ElMessage.error(res.data.message);
        }
      })
      .catch((err) => { });
  });
}
export function getoperator_list(group) {
  return new Promise(function (resolve, reject) {
    let operator_list = [];
    api
      .getListAPI("auth/user/operatorMap", {})
      .then((res) => {
        if (res.data.code == 1) {
          Object.keys(res.data.data).forEach((item) => {
            var index = Number(item);
            operator_list.push({
              value: index,
              name: res.data.data[item],
            });
          });
          resolve(operator_list);
        } else {
          ElMessage.error(res.data.message);
        }
        //数据处理
      })
      .catch((err) => {
        console.log(err);
      });
  });
}
export function bd_decrypt(bd_lng, bd_lat) {
  return new Promise(function (resolve, reject) {
    var X_PI = (Math.PI * 3000.0) / 180.0;
    var x = bd_lng - 0.0065;
    var y = bd_lat - 0.006;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
    var gg_lng = z * Math.cos(theta);
    var gg_lat = z * Math.sin(theta);
    var lnglat = [gg_lng, gg_lat];
    resolve(lnglat);
  });
}
export function school_list() {
  return new Promise(function (resolve, reject) {
    let schoolList = [];
    api
      .getListAPI("backup/area/areaTree", {})
      .then((res) => {
        if (res.data.code == 1) {
          let treelist = res.data.data;
          bbq: for (var i = 0; i < treelist.length; i++) {
            if (treelist[i].areaCode.length == 10 || treelist[i].areaCode.length == 6) {
              schoolList.push(treelist[i]);
            } else {
              ccc: for (var j = 0; j < treelist[i].child.length; j++) {
                ddd: for (
                  var z = 0;
                  z < treelist[i].child[j].child.length;
                  z++
                ) {
                  if (treelist[i].areaCode.length == 6) {
                    schoolList.push(treelist[i]);
                    break ccc;
                    break ddd;
                  } else if (treelist[i].child[j].areaCode.length == 6) {
                    schoolList.push(treelist[i].child[j]);
                    break ddd;
                  } else if (
                    treelist[i].child[j].child[z].areaCode.length == 6
                  ) {
                    schoolList.push(treelist[i].child[j].child[z]);
                  }
                }
              }
            }
          }
          resolve(schoolList);
        } else {
          ElMessage.error(res.data.message);
        }
      })
      .catch((err) => { });
  });
}
// 获取报警参数列表
export function getparametersList(areaCode) {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("backup/deviceConfig/list?areaCode=" + areaCode, {})
      .then((res) => {
        if (res.data.code == 1) {
          for (var i = 0; i < res.data.data.length; i++) {
            res.data.data[i].title = res.data.data[i].configName;
          }
          resolve(res.data.data);
        } else {
          ElMessage.error(res.data.message);
        }
      })
      .catch((err) => { });
  });
}
// 获取操作人
export function getOperatorList() {
  return new Promise(function (resolve, reject) {
    let operator = [];
    api
      .getListAPI("auth/user/operatorMap", "")
      .then((res) => {
        if (res.data.code == 1) {
          Object.keys(res.data.data).forEach((item) => {
            operator.push({ name: res.data.data[item], value: item });
          });
          resolve(operator);
        } else {
          ElMessage.error(res.data.message);
        }
        //数据处理
      })
      .catch((err) => {
        console.log(err);
      });
  });
}
// 获取项目列表

export function getProjectList() {
  return new Promise(function (resolve, reject) {
    let List = [];
    api
      .getListAPI("auth/sysProject/listAll", "")
      .then((res) => {
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else {
          ElMessage.error(res.data.message);
        }
        //数据处理
      })
      .catch((err) => {
        console.log(err);
      });
  });
}
// 获取项目列表

export function getIssueProjectList() {
  return new Promise(function (resolve, reject) {
    let List = [];
    api
      .getListAPI("auth/sysProject/listAll", "")
      .then((res) => {
        if (res.data.code == 1) {
          for (var i = 0; i < res.data.data.length; i++) {
            List.push({
              areaName: res.data.data[i].projectName,
              areaCode: res.data.data[i].id,
            });
          }
          console.log(List)
          resolve(List);
        } else {
          ElMessage.error(res.data.message);
        }
        //数据处理
      })
      .catch((err) => {
        console.log(err);
      });
  });
}
export function getAeaTreeList(projectId) {
  return new Promise(function (resolve, reject) {
    let List = [];
    api
      .getListAPI("backup/area/areaTreeByProject?projectId=" + projectId, {})
      .then((res) => {
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else {
          ElMessage.error(res.data.message);
        }
        //数据处理
      })
      .catch((err) => {
        console.log(err);
      });
  });
};
export function getlListByProdectId() {
  let projectId = Number(localStorage.getItem("projectId"));
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("backup/pseudoData/listByProdectId?projectId=" + projectId, "")
      .then((res) => {
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else {
          ElMessage.error(res.data.message);
        }
        //数据处理
      })
      .catch((err) => {
        console.log(err);
      });
  });
}
// 获取get
export function getAPI(url, obj) {
  const router = useRouter();
  return new Promise(function (resolve, reject) {
    api
      .getListAPI(url, obj)
      .then((res) => {
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else if (res.data.code == 401) {
          router.push("./login")
        }
      })
      .catch((err) => { });
  });
}
//树形菜单宽度拽拉
export function handleYResize() {
  const box = document.getElementsByClassName('boxMove');
  const left = document.getElementsByClassName('treeLeft');
  const resize = document.getElementsByClassName('y-resize');
  const right = document.getElementsByClassName('contentRight');
  for (let i = 0; i < resize.length; i++) {
    // 鼠标按下事件
    console.log(resize[i].style)
    resize[i].onmousedown = function (e) {
      // 颜色改变提醒
      resize[i].style.background = '#818181'
      const startX = e.clientX
      resize[i].left = resize[i].offsetLeft
      // 鼠标拖动事件
      document.onmousemove = function (e) {
        const endX = e.clientX
        let moveLen = resize[i].left + (endX - startX) // （endX-startX）=移动的距离。resize[i].left+移动的距离=左侧最终的高度
        const maxT = box[i].clientWidth - resize[i].offsetWidth // 容器宽度 - 左边区域的宽度 = 右边区域的宽度
        if (moveLen < 30) moveLen = 30 // left最小宽度度为30px
        if (moveLen > maxT - 30) moveLen = maxT - 30 // right最小宽度度为30px
        // console.log(moveLen)
        resize[i].style.left = moveLen // 设置left区域的宽度
        for (let j = 0; j < left.length; j++) {
          left[j].style.width = moveLen + 'px'
          right[j].style.width = (box[i].clientWidth - moveLen - 10) + 'px';
          window.addEventListener("resize", function () {
            right[j].style.width = (box[i].clientWidth - moveLen - 10) + 'px';
          });
        }
      }
      // 鼠标松开事件
      document.onmouseup = function (evt) {
        // 颜色恢复
        resize[i].style.background = '#C0C4CC'
        document.onmousemove = null
        document.onmouseup = null
        resize[i].releaseCapture && resize[i].releaseCapture() // 当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉
      }
      resize[i].setCapture && resize[i].setCapture() // 该函数在属于当前线程的指定窗口里设置鼠标捕获
      return false
    }
  }

}
// 地图
export function getAMapLoader2() {
  return new Promise(function (resolve, reject) {
    AMapLoader.reset();
    AMapLoader.load({
      key: "e6a43aa4581d0cb826ce01fb6f3c7eae", // 申请好的Web端开发者Key，首次调用 load 时必填
      version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
      plugins: ['AMap.Map', 'AMap.ControlBar', 'AMap.ToolBar'],
      Loca: {
        // 是否加载 Loca， 缺省不加载
        version: "2.0.0", // Loca 版本，缺省 1.3.2
      },
    })
      .then((AMap) => {
        resolve(AMap)
      })
      .catch((e) => {
        ElMessage.error(e.message);
      });
  });
} 